package top.cflwork.controller;

import com.xiaoleilu.hutool.date.DateUtil;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import top.cflwork.service.ShopCommentService;
import top.cflwork.util.PageUtils;
import top.cflwork.util.R;
import top.cflwork.vo.ShopCommentVo;

import java.util.List;

/**
 * 商品评论或留言
 * 
 * @author cfl
 * @email 275300091@qq.com
 * @date 2020-04-21 13:52:33
 */
 
@Controller
@RequestMapping("/shopComment")
public class ShopCommentController extends BaseController{
	@Autowired
	private ShopCommentService shopCommentService;
	
	@GetMapping("shopCommentPage")
	@RequiresPermissions("shopComment:shopCommentPage")
	public String ShopComment(){
	    return "/shopComment/shopComment";
	}

	@ResponseBody
	@PostMapping("/list")
	@RequiresPermissions("shopComment:list")
	public PageUtils list(ShopCommentVo shopCommentVo){
		shopCommentVo.setAdminId(getUserId());
		//查询列表数据
		List<ShopCommentVo> shopCommentList = shopCommentService.list(shopCommentVo);
		Long total = shopCommentService.count(shopCommentVo);
		PageUtils pageUtils = new PageUtils(shopCommentList, total);
		return pageUtils;
	}

    @ResponseBody
    @PostMapping("/listAll")
    @RequiresPermissions("shopComment:list")
    public List<ShopCommentVo> listAll(ShopCommentVo shopCommentVo){
		shopCommentVo.setAdminId(getUserId());
        //查询列表数据
        List<ShopCommentVo> shopCommentList = shopCommentService.list(shopCommentVo);
        return shopCommentList;
    }

	@GetMapping("/add")
	@RequiresPermissions("shopComment:add")
	public String add(){
	    return "/shopComment/add";
	}

	/*@GetMapping("/edit/{id}")
	@RequiresPermissions("shopComment:edit")
	public String edit(@PathVariable("id") String id,Model model){
		ShopCommentVo shopComment = shopCommentService.get(id);
		model.addAttribute("shopComment", shopComment);
	    return "/shopComment/edit";
	}*/

    @GetMapping("/edit/{id}")
    @RequiresPermissions("shopComment:edit")
    @ResponseBody
    public ShopCommentVo edit(@PathVariable("id") String id){
			ShopCommentVo shopComment = shopCommentService.get(id);
        return shopComment;
    }

	/**
	 * 保存
	 */
	@ResponseBody
	@PostMapping("/save")
	@RequiresPermissions("shopComment:add")
	public R save( ShopCommentVo shopComment){
		shopComment.setAdminId(getUserId());
		shopComment.setAdminName(getUsername());
		shopComment.setCreateTime(DateUtil.now());
		if(shopCommentService.save(shopComment)>0){
			return R.ok("新增成功");
		}
		return R.error("新增失败");
	}
	/**
	 * 修改
	 */
	@ResponseBody
	@RequestMapping("/update")
	@RequiresPermissions("shopComment:update")
	public R update( ShopCommentVo shopComment){
		shopCommentService.update(shopComment);
		return R.ok("修改成功");
	}

	/**
	 * 删除
	 */
	@PostMapping( "/remove")
	@ResponseBody
	@RequiresPermissions("shopComment:remove")
	public R remove( String id){
		if(shopCommentService.remove(id)>0){
		return R.ok("删除成功");
		}
		return R.error("删除失败");
	}

	/**
	 * 删除
	 */
	@PostMapping( "/batchRemove")
	@ResponseBody
	@RequiresPermissions("shopComment:batchRemove")
	public R remove(@RequestParam("ids[]") String[] ids){
		shopCommentService.batchRemove(ids);
		return R.ok("批量删除成功");
	}
    /**
     * 删除
     */
    @PostMapping( "/batchSave")
    @ResponseBody
    @RequiresPermissions("shopComment:batchSave")
    public R batchSave(List<ShopCommentVo> shopCommentList){
			shopCommentService.batchSave(shopCommentList);
        return R.ok("批量新增成功");
    }

	/**
	 * 保存
	 */
	@ResponseBody
	@PostMapping("/saveComment")
	public R saveComment( @RequestBody ShopCommentVo shopComment){
		shopComment.setAdminId(getUserId());
		shopComment.setAdminName(getUsername());
		shopComment.setCreateTime(DateUtil.now());
		if(shopCommentService.save(shopComment)>0){
			return R.ok("新增成功");
		}
		return R.error("新增失败");
	}
}
